package gui.obu;

import java.sql.SQLException;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.swtdesigner.SWTResourceManager;

public class ObuNewUserPanelImpl extends AbstractObuPanelImpl implements ObuNewUserPanel {
	
	private Text firstName;	
	private Label usernameLbl;
	private Button ok;
	private Text lastName;
	private Label collectFingerprintInst;
	private Button keyboard;
	private Label instructions;
	private Label lblFirstName;
	private Label lblLastName;
	
	public ObuNewUserPanelImpl(Display parent, Shell shell) {
		super(parent, shell);
	}

	/**
	 * Open the window.
	 */
	public void open() {
		// remove at compile time
//		Shell shell = new Shell();
		// ***********************
		
		shell.setSize(450, 300);
		shell.setText("New User");
		
		ok = new Button(shell, SWT.NONE);
		ok.setBounds(349, 229, 75, 25);
		ok.setText("OK");
		ok.addListener(SWT.Selection, new Listener() {
			@Override
			public void handleEvent(Event event) {
				ObuDrivingDisplayPanel window = new ObuDrivingDisplayPanelImpl(display, shell);
				
				try {
					database.addAlan();
				} catch (SQLException e) {
					e.printStackTrace();
				}
				
				dispose();
				window.open();
			}
		});
		
		usernameLbl = new Label(shell, SWT.NONE);
		usernameLbl.setBounds(10, 75, 120, 21);
		usernameLbl.setText("Driver's Name:");
		usernameLbl.setFont(new Font(display, "Arial", 12, SWT.BOLD));
		
		firstName = new Text(shell, SWT.BORDER);
		firstName.setBounds(136, 75, 140, 25);
		
		instructions = new Label(shell, SWT.CENTER);
		instructions.setBounds(10, 10, 414, 38);
		instructions.setText("Using the keyboard below,\n enter the new driver's name and press OK.");
		instructions.setFont(new Font(display, "Arial", 12, SWT.ITALIC));
		
		lastName = new Text(shell, SWT.BORDER);
		lastName.setBounds(282, 75, 140, 25);
		
		lblFirstName = new Label(shell, SWT.NONE);
		lblFirstName.setBounds(136, 54, 57, 15);
		lblFirstName.setText("First Name");
		
		lblLastName = new Label(shell, SWT.NONE);
		lblLastName.setBounds(282, 54, 57, 15);
		lblLastName.setText("Last Name");
		
		collectFingerprintInst = new Label(shell, SWT.NONE);
		collectFingerprintInst.setBounds(10, 233, 333, 21);
		collectFingerprintInst.setText("To collect fingerprint, please slide finger across reader.");
		collectFingerprintInst.setFont(new Font(display, "Arial", 10, SWT.ITALIC));
		
		keyboard = new Button(shell, SWT.NONE);
		keyboard.setBounds(29, 106, 378, 121);
		keyboard.setImage(SWTResourceManager.getImage(ObuNewUserPanelImpl.class, "/images/qwerty.gif"));
		
		shell.open();
		shell.layout();
	}

	@Override
	public String getFirstName() {
		return firstName.getText();
	}

	@Override
	public void setFirstName(String firstName) {
		this.firstName.setText(firstName);
	}
	
	@Override
	public String getLastName() {
		return lastName.getText();
	}
	
	public void setLastName(String lastName) {
		this.lastName.setText(lastName);
	}

	@Override
	public void addListener(int eventType, Listener listener) {
		ok.addListener(eventType, listener);
	}
}
